package com.chuang.bootplus.service.impl;

import cn.hutool.json.JSONException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuang.bootplus.base.exception.BusException;
import com.chuang.bootplus.base.utils.ApiResponse;
import com.chuang.bootplus.base.utils.BeanUtil;
import com.chuang.bootplus.base.utils.StringUtil;
import com.chuang.bootplus.entity.Sms;
import com.chuang.bootplus.entity.User;
import com.chuang.bootplus.mapper.SmsMapper;
import com.chuang.bootplus.po.SmsCheckPO;
import com.chuang.bootplus.service.SmsService;
import com.chuang.bootplus.service.UserService;
import com.chuang.bootplus.vo.SmsCheckVO;
import com.chuang.bootplus.vo.SmsVO;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SmsServiceImpl extends ServiceImpl<SmsMapper, Sms> implements SmsService {

    @Autowired
    UserService userService;

    @Override
    public ApiResponse<SmsVO> smsCode(Sms sms) {
        String phone = sms.getPhone();
        User user = userService.getOne(new QueryWrapper<User>().eq("phone", phone));
        sms.setCode(StringUtil.sixCode());
        sms.setMin(5);
        if(user == null){
            User userTemp = new User();
            userTemp.setCode(sms.getCode());
            userTemp.setPhone(phone);
            userService.save(userTemp);
        }else {
            user.setCode(sms.getCode());
            userService.removeById(user.getId());
            userService.save(user);
        }

        int appid = 1400589036;
        String appkey = "3add59cdd312e88dbbe2be0f174b6da2";
        int templateId = 1178966; //模板ID
        String smsSign = "AcSports公众号";
        try{
            String[] params = {sms.getCode(), Integer.toString(sms.getMin())}; //短信中的参数
            SmsSingleSender ssender = new SmsSingleSender(appid,appkey);

            SmsSingleSenderResult result = ssender.sendWithParam("86",sms.getPhone(),templateId,
                    params,smsSign,"","");
            System.out.println(result);
        }catch (HTTPException e){
            e.printStackTrace();
        }catch (JSONException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }

        SmsVO smsVO = BeanUtil.beanA2beanB(sms, SmsVO.class);
        smsVO.setMsg("验证码发送成功");

        return new ApiResponse<>(smsVO);
    }

    @Override
    public ApiResponse<SmsCheckVO> smsCheck(SmsCheckPO smsCheckPO) {
        User user = userService.getOne(new QueryWrapper<User>().eq("phone", smsCheckPO.getPhone()));

        if(!user.getCode().equals(smsCheckPO.getCode())){
            throw new BusException("验证码不正确!");
        }

        SmsCheckVO smsCheckVO = BeanUtil.beanA2beanB(smsCheckPO, SmsCheckVO.class);
        smsCheckVO.setMsg("验证码正确");
        return new ApiResponse<>(smsCheckVO);
    }
}
